package com.hmyd.hmyd_android.view;

import com.hmyd.hmyd_android.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 弧形textview
 * @author wangk
 *
 */
public class ArcText extends View {
	
	private TextPaint paint; // 文字画笔
	private TextPaint scorePaint; // 分数画笔
	private CharSequence content;
	private CharSequence score;
	private int contentColor;
	private int scoreColor;
	private float contentSize;
	private float scoreSize;
	private float orgAngle; // 初始角度
	private float alterAngle; // 角度改变幅度
	
	private int alterX;
	
	private int width; // 控件宽度
	private int height; // 控件高度
	
	private int widthMode; // 宽度模式。ex：warp_content,fill_parent
	private int heigjtMode; // 高度模式
	
	private RectF rectf;
	private Path path;
	
	
	public ArcText(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new TextPaint();
		scorePaint = new TextPaint();
		//this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);// 禁用硬件加速
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.arcTextAttr);
		content = "小主您今日获得积分";
		score = a.getText(R.styleable.arcTextAttr_contentScore);
		contentColor = a.getColor(R.styleable.arcTextAttr_contentColor, Color.YELLOW);
		scoreColor = a.getColor(R.styleable.arcTextAttr_contentScoreColor, Color.WHITE);
		contentSize = a.getDimension(R.styleable.arcTextAttr_contentSize, 12f);
		scoreSize = a.getDimension(R.styleable.arcTextAttr_contentScoreSize, 12f);
		a.recycle();
		paint.setColor(contentColor);
		paint.setTextSize(contentSize);
		paint.setFakeBoldText(true);// 加粗
	//	paint.setTextAlign(Align.CENTER);
		paint.setAntiAlias(true); // 扛锯齿
		paint.setStrokeWidth(2);
		paint.setStyle(Paint.Style.FILL); 
		
		scorePaint.setColor(scoreColor);
		scorePaint.setTextSize(scoreSize);
		scorePaint.setStrokeWidth(2);
		scorePaint.setFakeBoldText(true);// 加粗
		//scorePaint.setTextAlign(Align.CENTER);
		scorePaint.setAntiAlias(true); // 扛锯齿
		scorePaint.setStyle(Paint.Style.FILL); 
		if(content == null) {
			content = "";
		}
		if(score == null) {
			score = "0";
		}
		
		orgAngle = -11;
		alterAngle = 3;
		alterX = (int) contentSize;
		path = new Path();
	}

	public ArcText(Context context) {
		super(context);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Drawable d = getBackground();
		if(d != null) {
			d.draw(canvas);
		} else {
			canvas.drawColor(Color.RED);
		}
		// 计算文字路径rect高宽
		rectf = new RectF(0, getHeight()/2, getWidth(),getHeight()/2+contentSize*3);
		
		path.addArc(rectf,0, 360);
//		paint.setStyle(Paint.Style.STROKE);
//		canvas.drawPath(path, paint);
		float startX = getWidth()*3/2-contentSize*9/2-getScoreSize()/2;
		Log.e("arc", "startX:"+startX+":getWidth():"+getWidth()+",getPaddingLeft():"+getPaddingLeft());
		if(startX-getWidth()<getPaddingLeft()) {
			startX=startX+(getPaddingLeft()-(startX-getWidth()));
		}
//		float endX =startX+contentSize*9+getScoreSize();
//		if(getWidth()*2-endX-getPaddingLeft()-getPaddingRight()>getPaddingRight()) {
//			startX = startX+(getWidth()*2-endX-getPaddingLeft()-getPaddingRight()-getPaddingRight());
//		}
		canvas.drawTextOnPath("小主您今日获得", path, startX, 0, paint);
		canvas.drawTextOnPath(String.valueOf(getScore()), path,startX+contentSize*7, 0, scorePaint);
		canvas.drawTextOnPath("积分", path, startX+contentSize*7+getScoreSize(), 0, paint);
//		int paramX = (int) (getWidth()/2-(contentSize*(content.length()-1)+scoreSize)/2);
//		for (int i = 0; i < content.length(); i++) {
//			if(i == 7) {
//				paramX = (int) (paramX+scoreSize*(score.length()/4<1?1:score.length()/4));
//				drawText(canvas, score+"",paramX, getHeight()/2,scorePaint,  alterAngle);
//			}
//			if(i == 0) {
//				drawText(canvas, content.charAt(i), paramX, getHeight()/2, paint,  orgAngle);
//			} else if(i == 8){
//				paramX = (int) (paramX+scoreSize*(score.length()/4<1?1:score.length()/4));
//				drawText(canvas, content.charAt(i), paramX, getHeight()/2, paint,  alterAngle);
//			} else {
//				paramX = paramX+alterX;
//				drawText(canvas, content.charAt(i), paramX, getHeight()/2, paint,  alterAngle);
//			}
//		}
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// 获取宽高模式
		widthMode = MeasureSpec.getMode(widthMeasureSpec);
		heigjtMode = MeasureSpec.getMode(heightMeasureSpec);
		
		// 获取父容器传过来的宽高
		width = MeasureSpec.getSize(widthMeasureSpec);
		height = MeasureSpec.getSize(heightMeasureSpec);
		
		// 判断模式
		if(widthMode == MeasureSpec.EXACTLY) {
			widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
		} else {
			widthMeasureSpec = MeasureSpec.makeMeasureSpec((int)(contentSize*(content.length()+5)+scoreSize)
					, widthMode);
		}
		if(heigjtMode == MeasureSpec.EXACTLY) {
			heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
		} else {
			heightMeasureSpec = MeasureSpec.makeMeasureSpec((int)(contentSize*(content.length()-1)+10)/3+getPaddingTop()
					+getPaddingBottom(), heigjtMode);
		}
		
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	
	/**
	 * 根据指定的度数，在指定角度绘制文字
	 * @param canvas
	 * @param text
	 * @param x
	 * @param y
	 * @param paint
	 * @param angle
	 */
	private void drawText(Canvas canvas,char text,float x,float y,Paint paint,float angle) {
		if(angle != 0) {
			canvas.rotate(angle, x, y);
		}
		canvas.drawText(String.valueOf(text), x, y, paint);
	}
	
	/**
	 * 根据指定的度数，在指定角度绘制文字
	 * @param canvas
	 * @param text
	 * @param x
	 * @param y
	 * @param paint
	 * @param angle
	 */
	private void drawText(Canvas canvas,CharSequence text,float x,float y,Paint paint,float angle) {
		if(angle != 0) {
			canvas.rotate(angle, x, y);
		}
		canvas.drawText(String.valueOf(text), x, y, paint);
	}

	public CharSequence getContent() {
		return content;
	}

	public void setContent(CharSequence content) {
		this.content = content;
	}

	public CharSequence getScore() {
		return score;
	}

	public void setScore(CharSequence score) {
		this.score = score;
		invalidate();
	}

	public int getContentColor() {
		return contentColor;
	}

	public void setContentColor(int contentColor) {
		this.contentColor = contentColor;
	}

	public int getScoreColor() {
		return scoreColor;
	}

	public void setScoreColor(int scoreColor) {
		this.scoreColor = scoreColor;
	}

	public float getContentSize() {
		return contentSize;
	}

	public void setContentSize(float contentSize) {
		this.contentSize = contentSize;
	}

	public float getScoreSize() {
		return scoreSize;
	}

	public void setScoreSize(float scoreSize) {
		this.scoreSize = scoreSize;
	}

	public float getOrgAngle() {
		return orgAngle;
	}

	public void setOrgAngle(float orgAngle) {
		this.orgAngle = orgAngle;
	}

	public float getAlterAngle() {
		return alterAngle;
	}

	public void setAlterAngle(float alterAngle) {
		this.alterAngle = alterAngle;
	}

	public int getAlterX() {
		return alterX;
	}

	public void setAlterX(int alterX) {
		this.alterX = alterX;
	}


}
